package ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;

public abstract class AbstractUI extends Composite {
	
	protected Label title;
	protected TabFolder tabFolder;
	protected Label loadImage;
	protected Label loadText;
	protected volatile boolean isLoad;
	private boolean isVisible;

	public AbstractUI(Composite parent, int style) {
		super(parent, style);
		setBounds(70, 52, 1210, 636);
		
		title = new Label(this, SWT.NONE);
		title.setText("title");
		title.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 23, SWT.BOLD));
		title.setBounds(25, 18, 212, 51);
		
		tabFolder = new TabFolder(this, SWT.NONE);
		tabFolder.setBounds(25, 75, 1185, 42);
		
		loadText = new Label(this, SWT.CENTER);
		loadText.setText("加载中");
		loadText.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 23, SWT.BOLD));
		loadText.setBounds(499, 200, 212, 51);
		loadText.setVisible(false);
		
		loadImage = new Label(this, SWT.NONE);
		loadImage.setBounds(541, 260, 128, 128);
		loadImage.setVisible(false);
		setVisible(false);
		
		
	}
	public AbstractUI(Composite parent) {
		super(parent, SWT.NONE);
		setBounds(70, 52, 1210, 636);
		
		title = new Label(this, SWT.NONE);
		title.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		title.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 29, SWT.BOLD));
		title.setBounds(10, 16, 212, 51);
		title.setText("title");
		title.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 23, SWT.BOLD));
		title.setLocation(35, 18);

		setVisible(false);
	}
	
	@Override
	public void setVisible(boolean visible) {
		if (visible) {
			setBounds(120, 52, 1210, 636);
			super.setVisible(visible);
			slideIn();
			loadingAnimation();
		}
		else {
			super.setVisible(visible);
		}
		
	}
	
	protected void loadingAnimation() throws org.eclipse.swt.SWTException{
		
		new Thread() {
			public void run() {
				if (isLoad) {
					getDisplay().syncExec(new Runnable() {
						
						@Override
						public void run() {
							loadImage.setVisible(true);
							loadText.setVisible(true);
							loadImage.moveAbove(null);
							loadText.moveAbove(null);
						}
					});
					
					Image[] images = {
						SWTResourceManager.getImage("src/image/loading/0.png"),
						SWTResourceManager.getImage("src/image/loading/1.png"),
						SWTResourceManager.getImage("src/image/loading/2.png"),
						SWTResourceManager.getImage("src/image/loading/3.png"),
						SWTResourceManager.getImage("src/image/loading/4.png"),
						SWTResourceManager.getImage("src/image/loading/5.png"),
						SWTResourceManager.getImage("src/image/loading/6.png"),
						SWTResourceManager.getImage("src/image/loading/7.png")
						};
					int index = 0;
					isVisible = true;
					while (isLoad && isVisible) {
						index = (index + 1) & 7;
						int t = index;
						try {
							getDisplay().asyncExec(new Runnable() {	
								@Override
								public void run() {
									loadImage.setImage(images[t]);
									if (!isVisible()) {
										isVisible = false;
									}
								}
							});
						} catch (SWTException e) {
						}
						
						try {
							Thread.sleep(100L);
						} catch (InterruptedException e) {
						}
					}
					getDisplay().asyncExec(new Runnable() {
						
						@Override
						public void run() {
							loadImage.setVisible(false);
							loadText.setVisible(false);
						}
					});
				}
			};
		}.start();
	}
	
	private void slideIn() {
		new Thread() {
			public void run() {
				for(int i = 0; i <= 50; i += 5) {
					int t = i;
					getDisplay().asyncExec(new Runnable() {			
						@Override
						public void run() {
							setLocation(120 - t, 52);
						}
					});
					try {
						Thread.sleep(10L);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		}.start();	
	}
	
	public abstract void active();
}
